package com.cyam.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.cyam.domain.Article;
import com.cyam.domain.ArticleType;
import com.cyam.service.ArticleService;
import com.cyam.service.ArticleTypeService;
import com.cyam.util.Pager;

/**
 * 网站右侧
 * @author CourageAQ
 *
 */
@Controller
@RequestMapping("/right")
public class RightController {

	@Resource
	private ArticleTypeService articleTypeService;
	
	@Resource
	private ArticleService articleService;
	
	/**
	 * 右侧分类，返回分类
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/type")
	@ResponseBody
	public List<ArticleType> listArticleType() throws Exception{
		List<ArticleType> list = articleTypeService.findAll(null);
		return list;
	}
	
	/***
	 * 返回点击分类下的文章
	 * @param model
	 * @param id
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/viewtype")
	public String listArticleTypeList(Model model,@RequestParam(value = "id") int id) throws Exception{
		Map<String, Object> map = new HashMap<String,Object>();
		map.put("type", id);
		Pager pages = articleService.countPager(map,1, 8);
		model.addAttribute("pages", pages);
		model.addAttribute("type", id);
		return "view-type";
	}
	
	/****
	 * 分类下的文章分页
	 * @param model
	 * @param id
	 * @param page
	 * @param size
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/viewtypepage")
	@ResponseBody
	public JSONObject listArticleTypePage(Model model,@RequestParam(value = "id") int id,
			@RequestParam(value = "page",defaultValue = "1") int page,
			@RequestParam(value ="size",defaultValue = "8") int size) throws Exception{
		JSONObject result = new JSONObject();
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("type", id);
		Pager pages = articleService.findPager(map,page, size);
		result.put("result", pages);
		return result;
	}
	
	/***
	 * 返回近一年的博客个数分类
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/time")
	@ResponseBody
	public List<Article> viewTime() throws Exception {
		List<Article> list = articleService.selectByTime(null);
		return list;
	}
	
	/**
	 * 
	 * @param model
	 * @param id
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/viewtime")
	public String listArticleTimeList(Model model,@RequestParam(value = "article_time") String article_time) throws Exception{
		Map<String, Object> map = new HashMap<String,Object>();
		map.put("article_time", article_time);
		Pager pages = articleService.countByTime2(map,1, 8);
		model.addAttribute("pages", pages);
		model.addAttribute("article_time", article_time);
		return "view-time";
	}
	
	/***
	 * 时间分类
	 * @param model
	 * @param article_time
	 * @param page
	 * @param size
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/viewtimepage")
	@ResponseBody
	public JSONObject listArticleTimePage(Model model,@RequestParam(value = "article_time") String article_time,
			@RequestParam(value = "page",defaultValue = "1") int page,
			@RequestParam(value ="size",defaultValue = "8") int size) throws Exception{
		JSONObject result = new JSONObject();
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("article_time", article_time);
		Pager pages = articleService.selectByTime2(map,page, size);
		result.put("result", pages);
		return result;
	}
	
	/**
	 * 右侧返回top5数据
	 * @return
	 * @throws Exception 
	 */
	@RequestMapping("/top5")
	@ResponseBody
	public List<Article> listOfTimeTop5() throws Exception{
		List<Article> list = articleService.listOfTimeTop5();
		return list;
	}
	/**
	 * 右侧返回随机五条数据
	 * @return
	 * @throws Exception 
	 */
	@RequestMapping("/rand5")
	@ResponseBody
	public List<Article> listOfRand5() throws Exception{
		List<Article> list = articleService.listRandTop5();
		return list;
	}
	
}
